package com.smedi.ismedi.notification.core.application.service.impl;

import com.smedi.ismedi.notification.core.application.command.MailTemplateCommand;
import com.smedi.ismedi.notification.core.application.service.MailTemplateApplicationService;
import com.smedi.ismedi.notification.core.domain.aggregatemodel.MailTemplate;
import com.smedi.ismedi.notification.core.domain.service.MailTemplateDomainService;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @className: MailTemplateApplicationServiceImpl
 * @Description: TODO
 * @version: v1.0.0
 * @author: wzx
 * @date: 2022/11/23 13:19
 */

@Slf4j
@Service
@AllArgsConstructor
public class MailTemplateApplicationServiceImpl implements MailTemplateApplicationService {

    private final DozerUtil dozerUtil;

    private final MailTemplateDomainService mailTemplateDomainService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MailTemplate saveTemplate(MailTemplateCommand mailTemplateCommand) {
        MailTemplate mailTemplate = dozerUtil.convertor(mailTemplateCommand, MailTemplate.class);
        if (mailTemplate.getId() != null) {
            return updateTemplate((mailTemplate));
        } else {
            return newTemplate(mailTemplate);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTemplate(Long mailTemplateId) {
        mailTemplateDomainService.deleteTemplate(mailTemplateId);
    }


    private MailTemplate newTemplate(MailTemplate data) {
        return mailTemplateDomainService.createTemplate(data);
    }

    private MailTemplate updateTemplate(MailTemplate data) {
        return mailTemplateDomainService.updateTemplate(data);
    }

}